﻿using FlyHorse.LIMS.IBLL;
using FlyHorse.LIMS.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FlyHorse.LIMS.BLL
{
    public partial class SamplingPlaceBll : BaseBll<SamplingPlace>, ISamplingPlaceBll
    {

        public bool AddSamplingPlaceAndStandard(SamplingPlace entity)
        {
            using (OJCB4Entities db =new OJCB4Entities())
            {
                SamplingPlace samplingPlace = db.Set<SamplingPlace>().Add(entity);
                List<Good> goodsList = this.CurrentDBSession.GoodDal.LoadEntitiesAsNoTracking(m => m.GoodsType.GoodsTypeName == "产品").ToList();
                List<TestItem> testItemList = this.CurrentDBSession.SysReportTypeDal.LoadEntitiesAsNoTracking(m => m.SysReportTypeID == "2").First().TestItems.ToList();
                List<StandardSemi> standardList = new List<StandardSemi>();
                foreach (var goods in goodsList)
                {
                    foreach (var item in testItemList)
                    {
                        StandardSemi standard = new StandardSemi();
                        standard.StandardSemiID = Guid.NewGuid().ToString();
                        standard.FormOrder = item.Order;
                        standard.GoodsID = goods.GoodsID;
                        standard.MaxValue = null;
                        standard.MinValue = null;
                        standard.SamplingPlaceID = samplingPlace.SamplingPlaceID;
                        standard.TestItemID = item.TestItemID;
                        standardList.Add(standard);
                    }
                }
                foreach (var standard in standardList)
                {
                    int res = this.CurrentDBSession.StandardSemiDal.LoadEntitiesAsNoTracking(m => m.GoodsID == standard.GoodsID && m.SamplingPlaceID == standard.SamplingPlaceID && m.TestItemID == standard.TestItemID).Count();
                    if (res == 0)
                    {
                        db.Set<StandardSemi>().Add(standard);
                    }
                }
                int count = db.SaveChanges();
                if (count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public bool DeleteSamplingPlaceAndStandard(SamplingPlace entity)
        {
            using (OJCB4Entities db =new OJCB4Entities())
            {
                var model = db.SamplingPlaces.Find(entity.SamplingPlaceID);
                List<StandardSemi> standardList = db.StandardSemis.Where(m => m.SamplingPlaceID == entity.SamplingPlaceID).ToList();
                db.Set<SamplingPlace>().Remove(model);
                foreach (StandardSemi standard in standardList)
                {
                    db.Set<StandardSemi>().Remove(standard);
                }
                int count = db.SaveChanges();
                if (count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            
        }
    }
}
